﻿using Microsoft.EntityFrameworkCore;

namespace EFCoreLenarn
{
    public class TestDbContext : DbContext
    {
        public DbSet<Book> Books { get; set; }
        public DbSet<Author> Authors { get; set; }

        //public DbSet<Test> Tests { get; set; }
        /// <summary>
        /// 一对多关系 只添加一个类 另一个类会自动管理添加，也会创建到数据库中
        /// </summary>
        public DbSet<Article> Articles { get; set; }

        public DbSet<Comment> Comments { get; set; }

        public DbSet<User>  Users { get; set; }

        public DbSet<Leave>  Leaves { get; set; }

        public DbSet<Order>  Orders { get; set; }
        public DbSet<Delivery> Deliveries { get; set; }

        public DbSet<Student> Students { get; set; }
        public DbSet<Teacher> Teachers { get; set; }

        public DbSet<Configs> Configs { get; set; }
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            string conn = "Server=.;DataBase=demo1;Trusted_Connection=true";
            // string conn = _configuration.GetConnectionString("DefaultConnection");
            optionsBuilder.UseSqlServer(conn);

            // 打印执行的SQL语句
            optionsBuilder.LogTo(Console.WriteLine);

            // 添加批量删除插件
            optionsBuilder.UseBatchEF_MSSQL();
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            modelBuilder.ApplyConfigurationsFromAssembly(this.GetType().Assembly);
            // 当某个类需要配置忽略属性时候需要在这里配置
            //modelBuilder.Entity<Book>().Ignore(b => b.Name2);
        }
    }
}
